#include "Key.h"
Key_truth_table Key_previous_value;
Key_truth_table Key_current_value;

void S_1234567_GPIO_init(void)
{
	GPIO_InitTypeDef gpioinitstruct;
	RCC_AHBPeriphClockCmd(S_123_RCC, ENABLE);
	gpioinitstruct.GPIO_Pin	  = S_123_1_PIN | S_123_2_PIN | S_123_3_PIN;
	gpioinitstruct.GPIO_Mode = GPIO_Mode_IN;
	gpioinitstruct.GPIO_Drive = GPIO_Drive_3;
	gpioinitstruct.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(S_123_PORT, &gpioinitstruct);
	
	RCC_AHBPeriphClockCmd(S_4567_RCC, ENABLE);
	gpioinitstruct.GPIO_Pin	  = S_4567_4_PIN | S_4567_5_PIN | S_4567_6_PIN | S_4567_7_PIN;
	GPIO_Init(S_4567_PORT, &gpioinitstruct);
	Key_previous_value.Key_all = 0xFF;
	Key_current_value.Key_all = 0;
}


uint8_t Get_Key_value(void)
{
	return Key_current_value.Key_all;
}

void bsp_KeyScan20ms(void)
{
	Key_current_value.K_s_bit.s1 = GPIO_ReadInputDataBit(S_123_PORT, S_123_1_PIN) ^ Key_previous_value.K_s_bit.s1;
	if(Key_current_value.K_s_bit.s1 == GPIO_ReadInputDataBit(S_123_PORT, S_123_1_PIN))	
		Key_previous_value.K_s_bit.s1 = Key_current_value.K_s_bit.s1;

	Key_current_value.K_s_bit.s2 = GPIO_ReadInputDataBit(S_123_PORT, S_123_2_PIN) ^ Key_previous_value.K_s_bit.s2;
	if(Key_current_value.K_s_bit.s2 == GPIO_ReadInputDataBit(S_123_PORT, S_123_2_PIN))	
		Key_previous_value.K_s_bit.s2 = Key_current_value.K_s_bit.s2;
	
	Key_current_value.K_s_bit.s3 = GPIO_ReadInputDataBit(S_123_PORT, S_123_3_PIN) ^ Key_previous_value.K_s_bit.s3;
	if(Key_current_value.K_s_bit.s3 == GPIO_ReadInputDataBit(S_123_PORT, S_123_3_PIN))	
		Key_previous_value.K_s_bit.s3 = Key_current_value.K_s_bit.s3;	
	
	Key_current_value.K_s_bit.s4 = GPIO_ReadInputDataBit(S_4567_PORT, S_4567_4_PIN) ^ Key_previous_value.K_s_bit.s4;
	if(Key_current_value.K_s_bit.s4 == GPIO_ReadInputDataBit(S_4567_PORT, S_4567_4_PIN))	
		Key_previous_value.K_s_bit.s4 = Key_current_value.K_s_bit.s4;	
	
	Key_current_value.K_s_bit.s5 = GPIO_ReadInputDataBit(S_4567_PORT, S_4567_5_PIN) ^ Key_previous_value.K_s_bit.s5;
	if(Key_current_value.K_s_bit.s5 == GPIO_ReadInputDataBit(S_4567_PORT, S_4567_5_PIN))	
		Key_previous_value.K_s_bit.s5 = Key_current_value.K_s_bit.s5;

	Key_current_value.K_s_bit.s6 = GPIO_ReadInputDataBit(S_4567_PORT, S_4567_6_PIN) ^ Key_previous_value.K_s_bit.s6;
	if(Key_current_value.K_s_bit.s6 == GPIO_ReadInputDataBit(S_4567_PORT, S_4567_6_PIN))	
		Key_previous_value.K_s_bit.s6 = Key_current_value.K_s_bit.s6;

	Key_current_value.K_s_bit.s7 = GPIO_ReadInputDataBit(S_4567_PORT, S_4567_7_PIN) ^ Key_previous_value.K_s_bit.s7;
	if(Key_current_value.K_s_bit.s7 == GPIO_ReadInputDataBit(S_4567_PORT, S_4567_7_PIN))	
		Key_previous_value.K_s_bit.s7 = Key_current_value.K_s_bit.s7;	
}
